package ide.ccf.ccproject.wizards.pages;

import ide.ccf.ccproject.ProjectPlugin;
import ide.ccf.ccproject.common.Utils;
import ide.ccf.ccproject.preferences.CCFPreferencePage;
import ide.ccf.ccproject.preferences.PreferenceConstants;
import ide.ccf.ccproject.translate.Message;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PreferencesUtil;

/**
 * 非Qt项目导入向导页面
 *
 */
public class ImportNonQtWizardPage extends WizardPage implements
		SelectionListener, ModifyListener {
	Composite p;
	Text filename;
	Text filelocation;
	public Combo c_sdk;
	public Combo c_runtime;
	
	public ImportNonQtWizardPage(String pageName) {
		super(pageName);
		p = null;
		setTitle(pageName);
		setDescription(Message.ImportNonQtWizardPage_0);
		setPageComplete(false);
	}
	
    private Listener nameModifyListener = new Listener() {
        public void handleEvent(Event e) {
        	if(c_sdk.getText().isEmpty() || c_runtime.getText().isEmpty())
        		setPageComplete(false);
        	else
        		setPageComplete(true);
        }
    };

	public void createControl(Composite parent) {
		Composite workArea = new Composite(parent, SWT.NONE);
		setControl(workArea);
		p = parent;
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.verticalSpacing = 10;
		workArea.setLayout(layout);
		workArea.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
		
		final Group g1 = new Group(workArea,SWT.NONE);
		g1.setText(Message.ImportNonQtWizardPage_1);
		GridData gd1 = new GridData(GridData.FILL_BOTH);
		g1.setLayoutData(gd1);
		GridLayout _layout = new GridLayout(1,false);
		g1.setLayout(_layout);
		filename = new Text(g1, SWT.BORDER);
		filename.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		final Group g2 = new Group(workArea,SWT.NONE);
		g2.setText(Message.ImportNonQtWizardPage_2);
		GridData gd2 = new GridData(GridData.FILL_BOTH);
		g2.setLayoutData(gd2);
		GridLayout layout1 = new GridLayout(2,false);
		g2.setLayout(layout1);
		filelocation = new Text(g2, SWT.BORDER);
		filelocation.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		filelocation.addModifyListener(this);
		Button browse = new Button(g2, SWT.NONE);
		browse.setText(Message.UiFileWizardPage_11);
		browse.addSelectionListener(this);
		
		final Group g3 = new Group(workArea,SWT.NONE);
		g3.setText(Message.CCProjectWizardOnePage_0);
		GridData gd3 = new GridData(GridData.FILL_BOTH);
		gd3.horizontalSpan = 3;
		gd3.heightHint = 90;
		g3.setLayoutData(gd3);
		GridLayout layout2 = new GridLayout(2,false);
		layout2.verticalSpacing = 10;
		g3.setLayout(layout2);
		{
			Label l_1 = new Label(g3, SWT.NONE);
			l_1.setText(Message.CCProjectWizardOnePage_1);
			l_1.setFont(g3.getFont());
			l_1.setLayoutData(new GridData(GridData.BEGINNING));
			c_sdk = new Combo(g3, SWT.DROP_DOWN | SWT.READ_ONLY);
			c_sdk.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			c_sdk.setFont(g3.getFont());
			c_sdk.addListener(SWT.Modify, nameModifyListener);
			Label l_2 = new Label(g3, SWT.NONE);
			l_2.setText(Message.CCProjectWizardOnePage_2);
			l_2.setFont(g3.getFont());
			l_2.setLayoutData(new GridData(GridData.BEGINNING));
			c_runtime = new Combo(g3, SWT.DROP_DOWN | SWT.READ_ONLY);
			c_runtime.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			c_runtime.setFont(g3.getFont());
			c_runtime.addListener(SWT.Modify, nameModifyListener);
			
			String[] strRuntimeVersions = getCCFRuntimeVersions();
			String[] strSdkVersions = getCCFSdkVersions();
			if(strRuntimeVersions != null)
			{
				c_runtime.setItems(strRuntimeVersions);
				for(int i = 0; i < strRuntimeVersions.length; i++)
					c_runtime.setData(strRuntimeVersions[i]);
			}
			if(strSdkVersions != null)
			{
				c_sdk.setItems(strSdkVersions);
				for(int j = 0; j < strSdkVersions.length; j++)
					c_sdk.setData(strSdkVersions[j]);
			}
			c_sdk.setText(CCFPreferencePage.getCCFSdkVersion() == null ? "" : CCFPreferencePage.getCCFSdkVersion());
			c_runtime.setText(CCFPreferencePage.getCCFRuntimeVersion() == null ? "" : CCFPreferencePage.getCCFRuntimeVersion());
			
			Label l_occupy = new Label(g3, SWT.RIGHT);
			l_occupy.setText("");
			
			final Label l_3 = new Label(g3, SWT.RIGHT| SWT.HORIZONTAL );
			l_3.setText(Message.CCProjectWizardOnePage_3);
			l_3.setLayoutData(new GridData(GridData.FILL_BOTH));
			l_3.setFont(g3.getFont());
			Color color = new Color(null, 238, 99, 99);
			l_3.setForeground(color);
			l_3.addMouseTrackListener(new MouseTrackListener(){
				public void mouseEnter(MouseEvent e) {
					Color color = new Color(null, 139, 58, 58);
					l_3.setForeground(color);
					l_3.setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND));
				}
				public void mouseExit(MouseEvent e) {
					Color color = new Color(null, 238, 99, 99);
					l_3.setForeground(color);
				}
				public void mouseHover(MouseEvent e) {	
				}
			});
			l_3.addMouseListener(new MouseListener()
			{
				public void mouseDoubleClick(MouseEvent e) {
				}
				public void mouseDown(MouseEvent e) {
					PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(getShell(), "ide.ccf.ccproject.preferences.CCFPreferencePage", null, null);
					int type = dialog.open();
					if(type == dialog.OK)
					{
						c_sdk.setText(CCFPreferencePage.getCCFSdkVersion() == null ? "" : CCFPreferencePage.getCCFSdkVersion());
						c_runtime.setText(CCFPreferencePage.getCCFRuntimeVersion() == null ? "" : CCFPreferencePage.getCCFRuntimeVersion());
						//strRuntime = c_runtime.getText().trim();
						//strSdk = c_sdk.getText().trim();
					}
					else if(type == dialog.CANCEL)
					{
						
					}
				}
				public void mouseUp(MouseEvent e) {
				}
			});
		}

	}

	public void modifyText(ModifyEvent e) {
    	String s[] = filelocation.getText().trim().split("\\/");
    	filename.setText(s[s.length - 1]);
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO 自动生成的方法存根

	}

	public void widgetSelected(SelectionEvent e) {
		Shell shell = p.getShell();
		DirectoryDialog dlg = new DirectoryDialog(shell);
        dlg.setFilterPath("cccc");
        dlg.setText(Message.ImportNonQtWizardPage_3);
        dlg.setMessage(Message.ImportNonQtWizardPage_4);
        String dir = dlg.open();
        if (dir != null) 
        {
        	filelocation.setText(dir);
        	String s[] = dir.split("\\/");
        	filename.setText(s[s.length - 1]);
        }
        	setPageComplete(true);
	}

	public IPath getFileLocation() {
		return new Path(filelocation.getText().trim());
	}

	public String getProjectName() {
		return filename.getText().trim();
	}
	
	public static String[] getCCFSdkVersions() {
		if(ProjectPlugin.getDefault() != null)
		{
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			
			if (!store.contains(PreferenceConstants.CCFSDK_VERSIONS))
				return null;
			
			String defaultVersions = store.getString(PreferenceConstants.CCFSDK_VERSIONS);
			defaultVersions = defaultVersions.substring(1, defaultVersions.length()-1);
			//System.out.println("defaultVersions:"+defaultVersions);
			//cut string
			String[] b = defaultVersions.split(",");
			if(b.length > 0)
				return b;	
		}
		return null;
	}

	public static String[] getCCFRuntimeVersions() {
		if(ProjectPlugin.getDefault() != null)
		{
			IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
			
			if (!store.contains(PreferenceConstants.CCFRUNTIME_VERSIONS))
				return null;
			
			String defaultVersions = store.getString(PreferenceConstants.CCFRUNTIME_VERSIONS);
			defaultVersions = defaultVersions.substring(1, defaultVersions.length()-1);
			//System.out.println("defaultVersions:"+defaultVersions);
			//cut string
			String[] b = defaultVersions.split(",");
			if(b.length > 0)
				return b;	
		}
		return null;
	}

}
